package com.qqtech.qquan.sms.helper;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qqtech.core.common.constant.CoreConst;

public class SmsCfg {
	public static final Logger logger = LoggerFactory.getLogger(SmsCfg.class);
	private Properties props = null;
	private static SmsCfg instance = new SmsCfg();

	public static SmsCfg getInstance() {
		return instance;
	}

	/**
	 * 单例
	 */
	private static class Singleton {
		private static final SmsCfg instance = new SmsCfg();
	}

	private SmsCfg() {
		try {
			props = new Properties();
			InputStream reader = getClass().getClassLoader().getResourceAsStream("sms.properties");
			InputStreamReader in = new InputStreamReader(reader, CoreConst.DEFAULT_CODETYPE);
			props.load(in);
		} catch (Exception e) {
			logger.error("加载短信配置文件失败", e);
		}
	}

	/**
	 * 获取实例
	 * 
	 * @return
	 */
	public static SmsCfg getConfigHelper() {
		return Singleton.instance;
	}

	/**
	 * 获取值
	 * 
	 * @param key
	 * @return
	 */
	public String getValue(String key) {
		return props.getProperty(key).trim();
	}

	/**
	 * 根据key获取值
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public String getValue(String key, String defaultVal) {
		String val = props.getProperty(key);
		if (null == val)
			return defaultVal;
		return val;
	}

	public int getIntValue(String key, int defaultVal) {
		String val = props.getProperty(key);
		if (null == val)
			return defaultVal;
		return new Integer(val);
	}
}
